package com.zfl.thread.day05;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class VolatileDemo02 {

    private static volatile int num;

    private static AtomicInteger atomicInteger=new AtomicInteger(10);

    public static void addByAutomicInteger() {
        atomicInteger.incrementAndGet();
    }

    public  static void main(String[] args) throws InterruptedException {
        CountDownLatch c = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                try{
                for (int j = 0; j < 10000; j++) {
                    addByAutomicInteger();
                }}finally {
                    c.countDown();
                }

            }).start();
        }
        c.await();
//        Thread.sleep(1000);
        System.out.println("num = " + atomicInteger.get());
    }
}
